/**
 * 
 */
package com.haisen.lee.thread;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.haisen.lee.domain.MethodParameter;
import com.haisen.lee.util.WriteJavaScriptFile;

/**
 * @author Haisen Lee
 * 
 */
public class WriteJavaScriptFileThread extends Thread implements Runnable,
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1593692378143508049L;

	private static String methodSignature = "writeJavaScriptFile";

	private Class<WriteJavaScriptFile> writeJavaScriptFile = WriteJavaScriptFile.class;

	private List<MethodParameter> methodParameterList = new ArrayList<MethodParameter>();

	/**
	 * 
	 */
	public WriteJavaScriptFileThread() {
		super();
	}

	/**
	 * @param arg0
	 */
	public WriteJavaScriptFileThread(Runnable runnable) {
		super(runnable);
	}

	/**
	 * 
	 * @param str
	 */
	public WriteJavaScriptFileThread(String str) {
		super(str);
	}

	/**
	 * 
	 * @param threadGroup
	 * @param runnable
	 */
	public WriteJavaScriptFileThread(ThreadGroup threadGroup, Runnable runnable) {
		super(threadGroup, runnable);
	}

	/**
	 * 
	 * @param threadGroup
	 * @param str
	 */
	public WriteJavaScriptFileThread(ThreadGroup threadGroup, String str) {
		super(threadGroup, str);
	}

	/**
	 * 
	 * @param runnable
	 * @param str
	 */
	public WriteJavaScriptFileThread(Runnable runnable, String str) {
		super(runnable, str);
	}

	/**
	 * 
	 * @param threadGroup
	 * @param runnable
	 * @param str
	 */
	public WriteJavaScriptFileThread(ThreadGroup threadGroup,
			Runnable runnable, String str) {
		super(threadGroup, runnable, str);
	}

	/**
	 * 
	 * @param threadGroup
	 * @param runnable
	 * @param str
	 * @param lNumber
	 */
	public WriteJavaScriptFileThread(ThreadGroup threadGroup,
			Runnable runnable, String str, long lNumber) {
		super(threadGroup, runnable, str, lNumber);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void run() {
		if (this.methodParameterList.size() < 2)
			return;
		Class[] methodClassArray = new Class[this.methodParameterList.size()];
		Object[] methodParameterArray = new Object[this.methodParameterList
				.size()];
		for (int i = 0; i < this.methodParameterList.size(); i++) {
			methodClassArray[i] = this.methodParameterList.get(i)
					.getMethodParameterType();
			methodParameterArray[i] = this.methodParameterList.get(i)
					.getMethodParameterValue();
		}
		try {
			Method writeJavaScriptFileMethod = this.writeJavaScriptFile
					.getDeclaredMethod(methodSignature, methodClassArray);
			writeJavaScriptFileMethod.invoke(null, methodParameterArray);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	public List<MethodParameter> getMethodParameterList() {
		return methodParameterList;
	}

	public void setMethodParameterList(List<MethodParameter> methodParameterList) {
		this.methodParameterList = methodParameterList;
	}

}
